import axios from 'axios';
import router from '../router';
import { ElMessage } from 'element-plus';
const contentType = 'application/json;charset=UTF-8';
// var base = 'http://192.168.10.143:8111';
var base = "http://hnrkdt.com/api";

//监测环境如果是开发环境替换为'/apis',否则则由env定义
//跨域只有监测到/apis才会生效
// console.log(import.meta.env,'env');
// if (import.meta.env.VITE_API_ENV == 'developmen') {
// 	base='/apis'
// } else {
// 	base=process.env.VITE_API_BASE_URL
// }

const instance = axios.create({
	/**
	 * 是否携带cookie,注意若携带cookie后端必须配置
	 * 1.Access-Control-Allow-Origin为单一域名(具体到IP + port,用localhost貌似不行)
	 * 2.需要带上响应头Access-Control-Allow-Credentials
	 */
	// withCredentials: true,
	// timeout: 2000,
	baseURL: base,
	// baseURL: 'http://192.168.10.233:8110',
	headers: {
		'Content-Type': contentType
	}
});
// axios的全局配置
instance.defaults.headers.post = {
	'Content-Type': 'application/json'
};
instance.defaults.headers.common = {
	'Auth-Type': 'company-web',
	'X-Requested-With': 'XMLHttpRequest',
	token: 'sdfjlsdfjlsdjflsjflsfjlskd'
};
instance.defaults.headers['Access-Control-Allow-Origin'] = "*";
instance.defaults.headers['Access-Control-Allow-Methods'] = "GET,PUT,POST,DELETE,OPTIONS";

// 添加请求拦截器(post只能接受字符串类型数据)
instance.interceptors.request.use(
	config => {
		const token = localStorage.getItem('token');
		if (!token && config.url == '/user/login') {
			return config;
		} else {
			config.headers['token'] = token;
			return config;
		}
	},
	error => {
		return Promise.reject(error);
	}
);
const errorHandle = (status, other) => {
	switch (status) {
		case 400:
			ElMessage({
				type: 'warning',
				message: other.message
			});
			break;
		case 401:
			// @ts-nocheck
			console.log('认证失败');
			router.replace({
				name: 'login'
			});
			ElMessage({
				type: "warning",
				message: "登录过期,请重新登陆!",
			});
			break;
		case 403:
			console.log('token校验失败');
			router.replace({
				name: 'login'
			});
			ElMessage({
				type: "warning",
				message: "登录过期,请重新登陆!",
			});
			break;
		case 500:
			ElMessage({
				type: 'warning',
				message: other.message
			});
			break;
		case 404:
			console.log('请求的资源不存在');
			break;
		default:
			console.log(other);
			break;
	}
};

// 添加响应拦截器
instance.interceptors.response.use(
	// 响应包含以下信息data,status,statusText,headers,config
	res => {
		// if (res.data.code == 401) {
		// 	ElMessage({
		// 		type: "warning",
		// 		message: "登录过期,请重新登陆!",
		// 	});
		// 	router.replace({
		// 		name: 'main'
		// 	});
		// 	localStorage.removeItem('TeShu');
        //     localStorage.removeItem('role');
        //     localStorage.removeItem('token');
        //     localStorage.removeItem('unit');
		// }
		if (res.data && res.data.code !== 0 && !(res.data instanceof Blob)) {
			// console.log(res.data.message || '服务器出错!');
		}
		// 请求通用处理
		return res.data;
	},
	err => {
		// console.log(err);
		const { response } = err;
		if (response) {
			errorHandle(response.status, response.data);
			return Promise.reject(response);
		}
		// console.log('请求失败');
		return true;
	}
);

export default instance;